package com.scalpel.util.word;

import sun.misc.BASE64Encoder;
import sun.nio.ch.DirectBuffer;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.util.UUID;

/**
 *
 * Created by scalpel on 2017/2/8.
 */
public class ImageUtil {


    private String imageName;
    private short state = 0;// 0不需要创建图片 1需要创建

    private ByteBuffer dataBuffer;

    private InputStream in;
    private  int length = 0;

    //name前缀
    private final String namePrefix = "wordml://";


    /**
     * 根据文件创建图片
     * @param file
     */
    public  ImageUtil(File file) throws FileNotFoundException {
        this.state = 1;//创建图片
        this.imageName = namePrefix+getImageId();
        in = new FileInputStream(file);
        length = (int) file.length();
    }




    /**
     * 根据网络连接创建图片
     * @param url
     */
    public ImageUtil(URL url) throws IOException {
        this.state = 1;//创建图片
        this.imageName = namePrefix+getImageId();
        URLConnection connection = url.openConnection();
        in = connection.getInputStream();
        length = connection.getContentLength();
    }

    /**
     * 读取流信息
     * @param in
     * @throws IOException
     */
    public void readInputStream(InputStream in) throws IOException {


        dataBuffer = ByteBuffer.allocate(length);
        byte[] b =new byte[1024];
        int count = 0;
        while ((count = in.read(b)) !=-1)
        {
            dataBuffer.put(b,0,count);
        }
        in.close();
    }

    /**
     * 根据图片名称创建图片
     * @param imageName
     */
    public ImageUtil(String imageName) {
        this.imageName = imageName;
        this.state = 0;//不需要创建图片
    }

    /**
     *
     * @return
     * @throws IOException
     */
    public String getCreateImageBase64() throws IOException {
        readInputStream(in);
        BASE64Encoder encoder = new BASE64Encoder();
        StringBuilder xml = new StringBuilder();
        xml.append("<w:binData w:name=\"").append(imageName).append("\">");
        xml.append(encoder.encode(dataBuffer.array()));
        xml.append("</w:binData>");
        dataBuffer.clear();
        return xml.toString();
    }

    /**
     * 不带创建的image xml信息
     * @return
     */
    public String getImageXmlNoCreate() {
        StringBuilder xml = new StringBuilder();
        xml.append("    <w:r>");
        xml.append("        <w:rPr>");
        xml.append("            <w:rFonts w:hint=\"fareast\"/>");
        xml.append("            <w:lang w:val=\"EN-US\" w:fareast=\"ZH-CN\"/>");
        xml.append("        </w:rPr>");
        xml.append("        <w:pict>");
        xml.append("            <v:shape id=\"").append(getImageId()).append("\" style=\"height:200px;width:500px;\" o:spt=\"75\" alt=\"org1\" type=\"#_x0000_t75\" style=\"height:116.05pt;width:80pt;\" filled=\"f\" o:preferrelative=\"t\" stroked=\"f\" coordsize=\"21600,21600\">");
        xml.append("                <v:path/>");
        xml.append("                <v:fill on=\"f\" focussize=\"0,0\"/>");
        xml.append("                <v:stroke on=\"f\"/>");
        xml.append("                <v:imagedata src=\"").append(imageName).append("\" o:title=\"org1\"/>");
        xml.append("                <o:lock v:ext=\"edit\" aspectratio=\"t\"/>");
        xml.append("                <w10:wrap type=\"none\"/><w10:anchorlock/>");
        xml.append("            </v:shape>");
        xml.append("        </w:pict>");
        xml.append("    </w:r>");

        return xml.toString();
    }

    /**
     * 插入图片
     * @return
     * @throws IOException
     */
    public String getImageXml() throws IOException {
        StringBuilder xml = new StringBuilder();
        xml.append("    <w:r>");
        xml.append("        <w:rPr>");
        xml.append("            <w:rFonts w:hint=\"fareast\"/>");
        xml.append("            <w:lang w:val=\"EN-US\" w:fareast=\"ZH-CN\"/>");
        xml.append("        </w:rPr>");
        xml.append("        <w:pict>");

        /**
         * 创建图片
         */
        if(state == 1)
        {
            xml.append(getCreateImageBase64());
        }

        xml.append("            <v:shape id=\"").append(getImageId()).append("\" style=\"height:200px;width:500px;\" o:spt=\"75\" alt=\"org1\" type=\"#_x0000_t75\" style=\"height:116.05pt;width:80pt;\" filled=\"f\" o:preferrelative=\"t\" stroked=\"f\" coordsize=\"21600,21600\">");
        xml.append("                <v:path/>");
        xml.append("                <v:fill on=\"f\" focussize=\"0,0\"/>");
        xml.append("                <v:stroke on=\"f\"/>");
        xml.append("                <v:imagedata src=\"").append(imageName).append("\" o:title=\"org1\"/>");
        xml.append("                <o:lock v:ext=\"edit\" aspectratio=\"t\"/>");
        xml.append("                <w10:wrap type=\"none\"/><w10:anchorlock/>");
        xml.append("            </v:shape>");
        xml.append("        </w:pict>");
        xml.append("    </w:r>");

        return xml.toString();
    }

    /**
     * 返回插入图片的xml
     * @return
     */
    public String getLineImageXml() throws IOException {

        StringBuilder xml = new StringBuilder();
        xml.append("<w:p>");
        xml.append("    <w:pPr>");
        xml.append("        <w:rPr>");
        xml.append("            <w:rFonts w:hint=\"fareast\"/>");
        xml.append("            <w:lang w:val=\"EN-US\" w:fareast=\"ZH-CN\"/>");
        xml.append("        </w:rPr>");
        xml.append("    </w:pPr>");
        xml.append(getImageXml());
        xml.append("</w:p>");
        return xml.toString();
    }

    /**
     * 获取图片ID
     * 这里最好通过UUID生成
     * @return
     */
    public String getImageId()
    {
        return UUID.randomUUID().toString().replaceAll("-","");
    }



    public static void  main(String args[]) {
        BASE64Encoder encoder = new BASE64Encoder();
        System.out.println(encoder.encode("sssss".getBytes()));
    }


    public String getImageName() {
        return imageName;
    }
}
